﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using DataMode;
using System.Data;

namespace DAL
{
    public class Video : IVideo
    {
        DataModeDataContext ctx = new DataModeDataContext();

        public void InsertVideo(DataMode.video video)
        {
            ctx.video.InsertOnSubmit(video);
            ctx.SubmitChanges();
        }

        public void updateVideo(video newVideo)
        {
            var edit = ctx.video.SingleOrDefault<video>(s => s.video_id == newVideo.video_id);

            edit.video_name = newVideo.video_name;
            edit.video_description = newVideo.video_description;

            ctx.SubmitChanges();
        }

        public video findVideo(int id)
        {
            var edit = ctx.video.SingleOrDefault<video>(s => s.video_id == id);
            ctx.SubmitChanges();
            return edit;
        }

        public IOrderedQueryable GetAllVideos()
        {
            return from video in ctx.video
                   orderby video.load_time descending
                   select video;
        }

        public IOrderedQueryable GetVideosByAlbum(int albumID,string username)
        {
            return from video in ctx.video
                   where video.video_album_id == albumID &&
                   video.username == username
                   orderby video.load_time descending
                   select video;
        }

        public void DeleteVideo(int id)
        {
            var edit = ctx.video.Single<video>(s => s.video_id == id);
            if (edit != null)
            {
                ctx.video.DeleteOnSubmit(edit);
                ctx.SubmitChanges();
            }

        }

        public void RenameVideoByVideoID(int VideoID, string videoName)
        {
            DataMode.video edit = ctx.video.Single(s => s.video_id == VideoID);
            if (edit != null)
            {
                edit.video_name = videoName;
                ctx.SubmitChanges();
            }
            return;
        }

        public IOrderedQueryable GetHotVideo()
        {
            return (IOrderedQueryable)(from vid in ctx.video
                                       orderby vid.play_num descending
                                            select vid).Take(4);
        }
    }
}
